import { BlockVolume, Vector3 } from "@minecraft/server";
import { GameContext } from "@sapi-game/gameContext";
import { PlayerGroupSet } from "@sapi-game/gamePlayer/groupSet";
import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { Vector3Utils } from "@sapi-game/utils";
import { PKMapData } from "./maps";
import { parkourPlayer } from "./parkourPlayer";

interface PKConfig {
    /**地图 */
    map: PKMapData;
    /**建造时间 */
    buildTime: number;
    /**跑酷时间 */
    runTime: number;
    /**生命条数 */
    lifeCount: number;
    /**是否启用特殊方块 */
    blockEnabled: boolean;
}

type TeamColor = "red" | "blue";

interface TeamContext<P extends parkourPlayer = parkourPlayer> {
    team: PlayerGroup<P>;
    base: Vector3;
    volume: BlockVolume;
    score: number;
}

export class ParkourContext<
    P extends parkourPlayer = parkourPlayer
> extends GameContext {
    readonly teams: Record<TeamColor, TeamContext>;
    readonly groupSet: PlayerGroupSet<P>;
    /**出生点 */
    readonly spawn: Vector3 = { x: 754, y: -60, z: 594 };
    /**传入的配置 */
    readonly config: PKConfig;

    /**红队相关配置 */
    get red() {
        return this.teams.red;
    }

    /**蓝队相关配置 */
    get blue() {
        return this.teams.blue;
    }

    constructor(
        redTeam: PlayerGroup<P>,
        blueTeam: PlayerGroup<P>,
        config: PKConfig
    ) {
        super();
        this.config = config;

        const bases = {
            red: { x: 760, y: -59, z: 575 },
            blue: { x: 720, y: -59, z: 575 },
        };
        this.teams = {
            red: {
                team: redTeam,
                base: bases.red,
                volume: this.createMapVolume(bases.red, config.map),
                score: 0,
            },
            blue: {
                team: blueTeam,
                base: bases.blue,
                volume: this.createMapVolume(bases.blue, config.map),
                score: 0,
            },
        };
        this.groupSet = new PlayerGroupSet([redTeam, blueTeam]);
    }

    get map() {
        return this.config.map;
    }

    private createMapVolume(structureBase: Vector3, map: PKMapData) {
        const mapBase = Vector3Utils.add(structureBase, map.offset);
        return new BlockVolume(mapBase, Vector3Utils.add(map.volume, mapBase));
    }
}
